package com.datatest.riskcheck.controller;


import com.alibaba.fastjson.JSONObject;
import com.datatest.beans.po.ConditionEntity;
import com.datatest.beans.ResultEntity;
import com.datatest.beans.po.RuleEntity;
import com.datatest.beans.po.tongdun.DataCollectorEntity;
import com.datatest.beans.vo.FkRulesVo;
import com.datatest.riskcheck.service.api.PolicyService;
import com.datatest.riskcheck.service.api.RuleConditionService;
import com.datatest.riskcheck.service.api.RuleService;
import com.datatest.riskcheck.service.api.QuerySystemApiService;
import com.datatest.riskcheck.tools.CheckParam;
import com.datatest.riskcheck.tools.ExcelTools;
import jxl.read.biff.BiffException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/risktest/")
@RestController
public class RiskCheckTest {

    @Autowired
    private PolicyService policyService;

    @Autowired
    private RuleService ruleService;

    @Autowired
    private RuleConditionService ruleConditionService;

    @Autowired
    private FkRulesVo fkRulesVo;

    @Autowired
    private QuerySystemApiService querySystemApiService;

    @PostMapping("dataCheck")
    public Object dataCheck(@RequestBody Map<String,Object> params) throws BiffException, IOException {


//        throw new BusinessException(ErrorEnum.NO_PERMISSION.getErrorCode(),ErrorEnum.NO_PERMISSION.getEooroMsg());

        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(params);
        Map<String,String> resultMap = (Map) jsonObject.getJSONObject("hits").getJSONArray("hits").getJSONObject(0).getJSONObject("_source").get("activity");
        ResultEntity resultEntity = ExcelTools.readSpeColumns(new File("D:\\引擎指标检查文件\\小象优品项目\\小象优品.xls"),resultMap);
        resultEntity.setSeqId(jsonObject.getJSONObject("hits").getJSONArray("hits").getJSONObject(0).getString("_id"));

        return resultEntity;

    }

    @PostMapping("checkParam")
    public Object checkParam(@RequestBody Map<String,Object> params) throws IOException, InvalidFormatException {
        //JSONObject json = (JSONObject) JSONObject.toJSON(params);
        String sequenceId = params.get("sequenceId").toString();
        String s = CheckParam.PostApi(sequenceId);
        JSONObject json1 = JSONObject.parseObject(s);
        Map<String,String> resultMap = (Map) json1.getJSONObject("hits").getJSONArray("hits").getJSONObject(0).getJSONObject("_source").get("activity");
        FileInputStream fileInputStream = new FileInputStream(params.get("filePath").toString());
        ResultEntity resultEntity = CheckParam.redExecl(fileInputStream, params.get("sheetName").toString(), resultMap);
        fileInputStream.close();
        return resultEntity;
    }

    @PostMapping("selectByEventId")
    public FkRulesVo selectByEventId(@RequestBody Map<String,Object> params){

        /**
         * step:1 获取当前产品的规则集
                List<PolicyEntity> eventIdList = policyService.selectByEventId(params.get("eventId").toString());
        */
        //step:2 获取当前规则集下子规则集
        FkRulesVo fkRules = new FkRulesVo();
        List<RuleEntity> ruleList = ruleService.selectRules(params.get("eventId").toString());
            //step:3查询当前配置列表明细

            List<ConditionEntity> list = null;
            List<RuleEntity> rules = new ArrayList();
            RuleEntity rule = null;
            List<ConditionEntity> conditionList = ruleConditionService.selectAllRules(ruleList);

            for(RuleEntity ruleResult:ruleList){
                list = new ArrayList<>();
                rule = ruleResult;
                for(ConditionEntity codResult:conditionList){
                    if(codResult.getCondition_fk_rule_uuid().equals(ruleResult.getRule_uuid())){
                        list.add(codResult);
                    }
                }
                if(list.size() > 0) {
                    rule.setConditionEntityList(list);
                }
                rules.add(rule);
            }
            fkRules.setRuleEntityList(rules);
        return fkRules;
    }

    @RequestMapping("QuerySystemApi")
    public Object QuerySystemApi(@RequestBody Map<String,Object> params) throws IOException {
        List systemApiList = new ArrayList();
        List list = querySystemApiService.displayNameList(params.get("appName").toString(), params.get("eventId").toString());
        List<DataCollectorEntity> dataCollectorEntities = querySystemApiService.selectByDisplayName(list);
        return dataCollectorEntities;
    }
}
